<template>
  <el-container class="box">
    <el-header>
      <h1>登录界面</h1>
    </el-header>
    <el-main>
      <div class="content">
        <el-form status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="账号：">
            <el-input v-model.number="ruleForm.user"></el-input>
          </el-form-item>
          <el-form-item label="密码：">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            <el-radio v-model="ruleForm.radio" label="1">商家</el-radio>
            <el-radio v-model="ruleForm.radio" label="2">平台管理员</el-radio>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleForm.radio)">登陆</el-button>
            <el-button type="primary" @click="register">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      ruleForm: {
        pass: "",
        user: "",
        radio: "1"
      }
    };
  },
  methods: {
    submitForm(state) {
      if (state == 1) {
        axios
          .get("/pAdmins/login", {
            params: {
              merchantName: this.ruleForm.user,
              password: this.ruleForm.pass
            }
          })
          .then(res => {
            if (res.data.length != 0) {
              alert("登陆成功！");
              sessionStorage.setItem('id',res.data[0]._id)
              this.modifyMerchantId(res.data[0]._id);
              this.$router.push("/admin/index");
            }else{
              alert("账号无效")
            }
          });
      } else if (state == 2) {
        // sessionStorage.setItem('id',this.ruleForm.user)
        axios
          .get("/pAdmins/loginer", {
            params: { adminName: this.ruleForm.user, password: this.ruleForm.pass }
          })
          .then(({ data }) => {
            if(data.success){
              localStorage.token = data.token
              alert('登录成功')
              this.$router.push("/admin/adminIndex");
            }else{
              alert("您输入的账号或者密码不正确")
            }
          });
      }
    },
    // 注册跳转
    register() {
      this.$router.push("/admin/Register");
    }
  }
};
</script>

<style scoped>
html,
body {
  margin: 0px;
  padding: 0px;
}
.content {
  margin: 100px auto;
  width: 450px;
}
* {
  margin: 0px;
  padding: 0px;
}
.box {
  height: 100vh;
}
.ures {
  width: 200px;
}
.el-header {
  background-color: rgb(158, 157, 157);
  color: #333;
  text-align: center;
  line-height: 60px;
}
.el-main {
  background-color: #77777777;
  color: #333;
  text-align: center;
  line-height: 160px;
}
</style>